<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
require_once("content_negotiation/include.php");

/**
 * Aggregation
 * 
 * Redirect browsers to the HTML or RDF representation of an aggregation, using content negotiation.
 */
class escape_repositoryui_action_Aggregation extends escape_repositoryui_Action
{
	public function handleRequest()
	{
		/* @var $repository escape_repository_Repository */
		$repository =& $this->requestAttributes['repository'];
		/* @var $repositoryUi escape_repositoryui_RepositoryUi */
		$repositoryUi =& $this->requestAttributes['repositoryUi'];
		
		// fetch the ID from the request
		$aggregationId = $_REQUEST['actionValue'];
		$aggregationUri = $repository->convertDataObjectIdToObjectUri($aggregationId);
		
		// fetch the aggregation object
		$aggregation = null;
		try {
			$aggregation = $repository->getOreAggregationByUri($aggregationUri);
		}
		catch(Exception $exception)
		{
			// the specified aggregation does not exist
			header("HTTP/1.1 404 Not Found");
			header("Status: 404 Not Found");
			header('Content-Type: text/plain');
			print "Aggregation not found!\n";
			
			return null;
		}
		
		// perform content negotiation, unless the browser is MSIE (which has a broken accept header)
		$sendRdf = false;
		if(!escape_isBrowserInternetExplorer())
		{
			// prefer RDF/XML unless the client prefers (X)HTML
			$contentMatch = array(
								'type' => array(
												0 => 'application/xhtml+xml',
												1 => 'text/html',
												2 => 'application/rdf+xml',
											),
								'qFactorApp' => array(
												0 => 0.9,
												1 => 0.9,
												2 => 1,
											),
								);
			
			$bestMatch = conNeg::mimeBest($contentMatch, false);
			if($bestMatch == 'application/rdf+xml')
			{
				$sendRdf = true;
			}
		}
		
		
		// indicate to proxy servers that content negotiation has been performed
		header("Vary: Accept");
	
		// fetch the aggregation URL
		$aggregationUrl = $aggregation->getExternalUri();
		// fetch the resource map URL
		$remUrl = $aggregation->getRemExternalUri();
		if($sendRdf)
		{
			// client prefers RDF
			$url = $remUrl;
		}
		else
		{
			// client prefers (X)HTML
			$url = $repository->config['server_url'] . $repositoryUi->getActionLinkById('show', $aggregationId);
		}
		// add link to aggregation to headers
		header('Link: <' . $aggregationUrl . '>; rel="aggregation"', false);
		// add link to resource map to headers
		header('Link: <' . $remUrl . '>; type="application/rdf+xml"; rel="resourcemap"', false);
		// perform the redirect
		header('Location: ' . $url, true, 303);

		return null;
	}
}